package com.company.ljh.medium;

/**
 * @description:
 * 162. 寻找峰值
 * @projectName:leet_code
 * @see:com.company.ljh.medium
 * @author:ljh
 * @createTime:2023/10/18 10:15
 * @version:1.0
 */
public class 寻找峰值 {
    //题目默认边界值大于侧边，因此当前数右边（左边）的数比当前大，则当前当前数右边（左边）一定存在峰值，可以直接舍弃左边（右边）
    public int findPeakElement(int[] nums) {
        return middleSearch(nums,0,nums.length);
    }
    //二分法
    public int middleSearch(int [] nums,int start,int end){
        int middle = start + (end-start)/2;
        int left = Integer.MIN_VALUE;
        int right = Integer.MIN_VALUE;
        if(middle != 0){
            left = nums[middle - 1];
        }
        if(middle != nums.length-1){
            right = nums[middle + 1];
        }
        if(nums[middle] >= left && nums[middle] >= right){
            return middle;
        }
        if(right > nums[middle] ){
            return middleSearch(nums,middle + 1,end);
        }
        if(left > nums[middle] ){
            return middleSearch(nums,start,middle - 1);
        }
        return -1;
    }
}
